//
//  GeometryReaderPageView.swift
//  SwiftUIDemo
//
//  Created by zhouzuosong on 2020/8/18.
//  Copyright © 2020 Kael. All rights reserved.
//

import SwiftUI

struct GeometryReaderPageView: View {
    var body: some View {
        VStack(spacing:20) {
            Text("GeometryReader").font(.title)
            Text("几何阅读器").font(.subheadline)
            Text("GeometryReader是一个容器视图，它推出来填满所有可用空间。您使用它来帮助在其中定位项目。")
                .font(.headline)
                .foregroundColor(.gray)
                .padding()
            GeometryReader{ geometry in
            
                Text("GeometryReader 里面的视图是默认居中的吗?")
                    .frame(maxWidth: 300)
                    .font(.title)
                    .padding()
                    .background(Color.gray)
                
                Text("Top Left")
                    .font(.title)
                    .padding()
                    .background(Color.pink)
                    .position(x: geometry.size.width/4, y: geometry.size.height/5)
                    
                
                Text("Bottom right")
                    .font(.title)
                    .position(x: geometry.size.width*4/5, y: geometry.size.height*7/8)

            }
            .frame(maxHeight: 180)
            .foregroundColor(.white)
            .background(Color.pink)
            
            GeometryReader{ geometry in
                
                VStack {
                    Text("X: \(geometry.frame(in: .global).origin.x)")
                    Text("Y: \(geometry.frame(in: .global).origin.y)")
                }
                .padding()
                .background(Color.gray)

            }
            .frame(maxHeight: 80)
            .foregroundColor(.white)
            .background(Color.pink)
            
            GeometryReader{ geometry in
                
                VStack(alignment: .center, spacing: 10) {
                    Text("frame in local: 获取X Y")
                        .font(.title)
                        .foregroundColor(.white)
                    HStack {
                       
                        Text("minX: \(Int(geometry.frame(in: .local).minX))")
                        Spacer()
                        Text("midX: \(Int(geometry.frame(in: .local).midX))")
                        Spacer()
                        Text("maxX: \(Int(geometry.frame(in: .local).maxX))")
                    }
                    .padding()
                    .background(Color.green)
                    
                    VStack {
                       
                        Text("minY: \(Int(geometry.frame(in: .local).minY))")
                        Spacer()
                        Text("midY: \(Int(geometry.frame(in: .local).midY))")
                        Spacer()
                        Text("maxY: \(Int(geometry.frame(in: .local).maxY))")
                    }
                    .padding()
                    .background(Color.orange)
                    
                    VStack {
                        Text("safeAreaInsets.leading: \(geometry.safeAreaInsets.leading)")
                        Text("safeAreaInsets.trailing: \(geometry.safeAreaInsets.trailing)")
                        Text("safeAreaInsets.top: \(geometry.safeAreaInsets.top)")
                        Text("safeAreaInsets.bottom: \(geometry.safeAreaInsets.bottom)")
                    }
                    
                }

            }
            .foregroundColor(.white)
            .background(Color.pink)
            
//            GeometryReader { geometry in    VStack {
//                Text("safeAreaInsets.leading: \(geometry.safeAreaInsets.leading)")
//                Text("safeAreaInsets.trailing: \(geometry.safeAreaInsets.trailing)")
//                Text("safeAreaInsets.top: \(geometry.safeAreaInsets.top)")
//                Text("safeAreaInsets.bottom: \(geometry.safeAreaInsets.bottom)")
//
//            }}
//            .font(.title).background(Color.pink).foregroundColor(.white)
            
//            Circle()
//                .fill(Color.green)
//                .overlay(Text("Touch Me").foregroundColor(.white).font(.title))
        }
    }
}

struct GeometryReaderPageView_Previews: PreviewProvider {
    static var previews: some View {
        GeometryReaderPageView()
            .previewDevice(PreviewDevice(rawValue: "iPhone X"))

    }
}
